//
//  MAPYServersListViewController.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYServersListViewController: MAPYBaseCollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        mapy_confiListData()
        NotificationCenter.default.addObserver(self, selector: #selector(mapy_updateList), name: mapy_vpnlistUpdate_noti, object: nil)
        
        
    }
    // MARK: - mapy action
    override func mapy_layout() -> UICollectionViewFlowLayout {
        let mapy_layout = UICollectionViewFlowLayout()
        mapy_layout.itemSize = CGSize(width: mapy_SCREEN_WIDTH-28, height: 68)
        mapy_layout.headerReferenceSize = CGSize(width: mapy_SCREEN_WIDTH-28, height: 58)
        mapy_layout.minimumLineSpacing = 5
        return mapy_layout
    }
    override func mapy_collectionViews() {
        super.mapy_collectionViews()
        view.addSubview(mapy_navView)
        mapy_collectionView.snp.makeConstraints { make in
            make.left.width.bottom.equalToSuperview()
            make.top.equalTo(mapy_navView.snp.bottom)
        }
       
        mapy_collectionView.register(MAPYListHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MAPYListHeadView.mapy_viewToString)
        mapy_collectionView.dataSource = self
        mapy_collectionView.delegate = self
        mapy_collectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: MAPYCommon.mapy_tabbarH+15, right: 14)
        mapy_collectionView.register(MAPYListCell.classForCoder(), forCellWithReuseIdentifier: MAPYListCell.mapy_viewToString)
    }
    func mapy_confiListData() {
        MAPYManager.mapy_manager.mapy_updateDataBaseSmartAddressNoPing()
        let mapy_smartModel = MAPYManager.mapy_manager.mapy_getSmartServerModel()
        if let mapy_smartModel = mapy_smartModel {
            mapy_serverModels.append([mapy_smartModel])
        }
        let mapy_serModels = MAPYManager.mapy_manager.mapy_getAllServerModels()
        if mapy_serModels.count == 0 {
            MAPYManager.mapy_manager.mapy_updateDataBaseListAddress()
            let mapy_serModels2 = MAPYManager.mapy_manager.mapy_getAllServerModels()
            if mapy_serModels2.count != 0 {
                mapy_serverModels.append(mapy_serModels2)
            }
        }else {
            mapy_serverModels.append(mapy_serModels)
        }
//        MAPYManager.mapy_manager.mapy_ipInfoVPNAction(mapy_serModels: mapy_serModels)
       
    }
    @objc func mapy_updateList() {
        if mapy_serverModels.count == 0 || mapy_serverModels.count == 1 {
          mapy_serverModels.removeAll()
            mapy_confiListData()
          mapy_collectionView.reloadData()
        }
       
    }
    // MARK: - mapy lazy
    override func mapy_baseBgName() -> String? {
        return "mapy_list_bg"
    }
    lazy var mapy_navView: MAPYServerConnectNavView = {
        let mapy_navView = MAPYServerConnectNavView()
        mapy_navView.mapy_titleLabel.text = "Servers List"
        return mapy_navView
    }()
    var mapy_serverModels = [[MAPYServerModel]]()
//    var mapy_selectIndexPath = IndexPath(item: 0, section: 0)
}
extension MAPYServersListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return mapy_serverModels.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return mapy_serverModels[section].count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYListCell.mapy_viewToString, for: indexPath) as! MAPYListCell
        let mapy_serverModel = mapy_serverModels[indexPath.section][indexPath.row]
        if indexPath.section == 0 {
            mapy_cell.mapy_bgView.image = UIImage(named: "mapy_list_smart")
//            mapy_cell.mapy_iconView.image = UIImage(named: "mapy_home_changeIcon")
        }else {
            mapy_cell.mapy_bgView.image = UIImage(named: "mapy_list_other")
        }
        
        mapy_cell.mapy_serverModel = mapy_serverModel
        
        return mapy_cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let mapy_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: MAPYListHeadView.mapy_viewToString, for: indexPath) as! MAPYListHeadView
        if indexPath.section == 0 {
            mapy_headView.mapy_titleLabel.text = "Smart location"
        }else {
            mapy_headView.mapy_titleLabel.text = "Other location"
        }
        return mapy_headView
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        MAPYManager.mapy_manager.mapy_updateSelectServerModels()
//        mapy_serverModels[mapy_selectIndexPath.section][mapy_selectIndexPath.row].mapy_select = false
          let mapy_serverModel = mapy_serverModels[indexPath.section][indexPath.row]
//        if mapy_serverModel.mapy_smart {
//            MAPYManager.mapy_manager.mapy_connectSmartAction()
//        }else {
//        mapy_serverModel.mapy_select = true
//          MAPYManager.mapy_manager.mapy_connectListAction(mapy_serverModel: mapy_serverModel)
//        }
        mapy_serverModel.mapy_select = true
        MAPYManager.mapy_manager.mapy_updateDataModel()
        MAPYManager.mapy_manager.mapy_connectSelectAddreddAction()
        navigationController?.popToRootViewController(animated: true)
        MAPYManager.mapy_manager.mapy_smart = false
        MAPYManager.mapy_manager.mapy_best = false
        
    }
}
